/*
 * @author: wizardc
 */

using UnityEngine;

namespace Dou.ScreenController
{
    /// <summary>
    /// 将自身尺寸控制在刘海屏安全区域内的适配器
    /// </summary>
    [RequireComponent(typeof(RectTransform))]
    public class SafeAreaAdapter: MonoBehaviour, IUIAdapter
    {
        private RectTransform _rectTransform;
        private Vector2 _offsetMin;
        private Vector2 _offsetMax;
        
        void Awake()
        {
            _rectTransform = GetComponent<RectTransform>();
            _offsetMin = _rectTransform.offsetMin;
            _offsetMax = _rectTransform.offsetMax;
            ScreenUIAdapterMgr.instance.AddAdapter(this);
        }
        
        public void RefreshAdapter(int uiScreenMode, int uiFineTuningMode)
        {
            var adapterMgr = ScreenUIAdapterMgr.instance;
            
            var safeAreaOffset = adapterMgr.safeAreaOffsetOnlyOneSide;
            _rectTransform.offsetMin = _offsetMin + new Vector2(safeAreaOffset.x, safeAreaOffset.y);
            _rectTransform.offsetMax = _offsetMax - new Vector2(safeAreaOffset.z, safeAreaOffset.w);
        }

        void OnDestroy()
        {
            ScreenUIAdapterMgr.instance.RemoveAdapter(this);
        }
    }
}
